package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import com.sinog.front.config.idempotent.Idempotent;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.AzgzInfoService;
import com.sinog.front.service.user.UserFacade;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 安置工作信息管理 PrisonerSettleController
 * @author sunsf
 * @date 2020-02-07-13:51
 */
@RestController
public class PrisonerSettleController {

    @Resource
    private UserFacade userService;
    @Resource
    private AzgzInfoService azgzInfoService;

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置 - 列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param request request
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/azgzgl/getUnSettleList")
    public String getTuoguanList(Integer pageIndex,Integer pageSize,String sortField,String sortOrder,HttpServletRequest request) {
        String key = request.getParameter("querykey");
        String pinggu = request.getParameter("pinggu");
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return azgzInfoService.getUnSettleList(pageIndex,pageSize,key,departId,level,orgid,pinggu,sortField,sortOrder);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param request request
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/azgzgl/getDownSettleList")
    public String getDownSettleList(Integer pageIndex,Integer pageSize,String sortField,String sortOrder,HttpServletRequest request) {
        String key = request.getParameter("querykey");
        String pinggu = request.getParameter("pinggu");
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return azgzInfoService.getDownSettleList(pageIndex,pageSize,key,departId,level,orgid,pinggu,sortField,sortOrder);
    }

    /**
     * 山西个性化
     * 安置帮教管理 - 安置工作信息管理 - 已移出 - 列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @param request request
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/azgzgl/getMoveSettleList")
    public String getMoveSettleList(Integer pageIndex,Integer pageSize,String sortField,String sortOrder,HttpServletRequest request) {
        String key = request.getParameter("querykey");
        String pinggu = request.getParameter("pinggu");
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return azgzInfoService.getMoveSettleList(pageIndex,pageSize,key,departId,level,orgid,pinggu,sortField,sortOrder);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 删除
     * @param request request
     * @param delId 安置人员编号
     * @return Result
     */
    @Log(type = "删除", value = "删除安置信息")
    @Idempotent(paramPos = {1})
    @RequestMapping("/azbj/azbjgl/azgzgl/view/delAzInfoAction")
    public Result delAzInfoAction(HttpServletRequest request,
                                  @RequestParam(required = false, defaultValue = "-1") String delId) {
        return azgzInfoService.delAzInfoAction(delId);
    }

    /**
     * 获取培训机构下拉列表（获取培训范围是本省、本市、本区县、本街道的所有培训机构）
     * @return List<Map<String,Object>>
     */
    @RequestMapping("/azbj/azbjgl/azgzgl/getPxjgList")
    public List<Map<String,Object>> getPxjgList() {
        UserDTO user = userService.getAuthentication();
        return azgzInfoService.getPxjgList(user);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 根据帮教基地类型查询具体条目
     * @param azType 基地类型
     * @param priNum 服刑人员编号
     * @return List<Map<String,Object>>
     */
    @RequestMapping("/azbj/azbjgl/azgzgl/getAzjdList")
    public List<Map<String,Object>> getAzjdList(
            @RequestParam(required = false, defaultValue = "-1") String azType,
            @RequestParam(required = false, defaultValue = "-1") String priNum) {
        UserDTO user = userService.getAuthentication();
        return azgzInfoService.getAzjdList(azType,user,priNum);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 就业推荐列表
     * @param priNum 服刑人员编号
     * @return List<Map<String,Object>>
     */
    @RequestMapping("/azbj/azbjgl/azgzgl/getJytjList")
    public String getJytjList(@RequestParam(required = false, defaultValue = "-1") String priNum,
                                                @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return azgzInfoService.getJytjList(pageIndex,pageSize,priNum,departId,level,orgid);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 基地推荐列表
     * @param priNum 服刑人员编号
     * @return List<Map<String,Object>>
     */
    @RequestMapping("/azbj/azbjgl/azgzgl/getJdtjList")
    public String getJdtjList(@RequestParam(required = false, defaultValue = "-1") String priNum,
                                                @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                                @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        return azgzInfoService.getJdtjList(departId,level,priNum,pageIndex,pageSize);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 待安置/已安置 - 安置 - 保存
     * @param request request
     * @param data 业务数据
     * @return Result
     */
    @Log(type = "新增", value = "安置工作功能操作，参数中有id为编辑【更新】，没id为新增【新增】")
    @Idempotent(paramPos = 1)
    @PostMapping("/azbj/azbjgl/azgzgl/saveAzInfo")
    public Result saveAzInfo(HttpServletRequest request,
                             @RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        //取数据
        String edit = JsonUtil.getValueFromJsonstrByKey(data, "edit");
        String jdid = JsonUtil.getValueFromJsonstrByKey(data, Constants.StrFinalValue.JDID);
        String priNum = JsonUtil.getValueFromJsonstrByKey(data, "priNum");
        if(!StringUtils.isEmpty(jdid)) {
            Map<String,Integer> jdanzhi = searchAnZhiCount(jdid);
            //判断该安置人数是否存在该基地
            Integer count = azgzInfoService.searchAnzhiByPriNum(priNum,jdid);
            if(jdanzhi != null && Constants.Number.NUM_ZERO < count) {
                //如果count > 0则说明该人员存在于此基地
                data = JsonUtil.setObjectToJsonStr(data, "anzhirenshu",jdanzhi.get("anzhirenshu"));
            } else if(jdanzhi != null) {
                //如果此人不存在此基地，则人数+1
                data = JsonUtil.setObjectToJsonStr(data, "anzhirenshu",jdanzhi.get("anzhirenshu") + 1);
            }
        }
        Date date = new Date();
        // 修改
        if(Constants.StrFinalValue.TRUEVALUE.equals(edit)) {
            data = JsonUtil.setObjectToJsonStr(data, "updateTime",date,"updateUser",user.getUsername());
        } else {
            data = JsonUtil.setObjectToJsonStr(data, "adduserid",user.getDepartid(),"createTime",date,"createUser",user.getUsername(),"uploadFlag",Constants.Number.NUM_ZERO,"updateTime",date,"updateUser",user.getUsername());
        }
        return azgzInfoService.saveAzInfo(data);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 移出基地操作
     * @param priNum 服刑人员编号
     * @return Result
     */
    @Log(type = "更新", value = "移出基地操作")
    @Idempotent()
    @RequestMapping("/azbj/azbjgl/azgzgl/moveJdCheckPriNum")
    public Result moveJdCheckPriNum(@RequestParam(required = false, defaultValue = "-1") String priNum) {
        Result result = azgzInfoService.moveJdCheckPriNum(priNum);
        if(ObjectUtils.allNotNull(result) && ObjectUtils.allNotNull(result.getData())) {
            return azgzInfoService.changeAnzhiState(priNum);
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 批量移出基地操作
     * @param priNums 服刑人员编号(多个priNum用"|"拼接)
     * @return Result
     */
    @Log(type = "更新", value = "批量移出基地操作")
    @Idempotent()
    @RequestMapping("/azbj/azbjgl/azgzgl/moveJdCheckPriNumPl")
    public Result moveJdCheckPriNumPl(@RequestParam(required = false, defaultValue = "-1") String priNums) {
        return azgzInfoService.moveJdCheckPriNumPl(priNums);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 移除 - 移至其他基地
     * @param data 业务数据
     * @return Result
     */
    @Log(type = "更新", value = "移至其他基地")
    @Idempotent()
    @RequestMapping("/azbj/azbjgl/azgzgl/moveJdBypriNum")
    public Result moveJdBypriNum(@RequestParam(required = false, defaultValue = "-1") String data) {
        return azgzInfoService.moveJdBypriNum(data);
    }

    /**
     * 安置帮教管理 - 安置工作信息管理 - 已安置 - 移除 - 批量移至其他基地
     * @param data 业务数据
     * @return Result
     */
    @Log(type = "更新", value = "批量移至其他基地")
    @Idempotent()
    @RequestMapping("/azbj/azbjgl/azgzgl/moveJdBypriNumPl")
    public Result moveJdBypriNumPl(@RequestParam(required = false, defaultValue = "-1") String data) {
        return azgzInfoService.moveJdBypriNumPl(data);
    }

    /**
     * 查询当前基地已安置人数
     * @param jdid 基地编号
     * @return Map<String,String>
     */
    @PostMapping("/azbj/azbjgl/azgzgl/searchAnZhiCount")
    public Map<String,Integer> searchAnZhiCount(@RequestParam(required = false, defaultValue = "-1") String jdid) {
        return azgzInfoService.selectKeAnZhiNum(jdid);
    }
}